package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam Integer[] checkitemIds) {
        if (checkGroupService.add(checkGroup, checkitemIds)) {
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }else {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据id 删除检查组和关联的检查项
     *
     * @param id
     * @return
     */
    @GetMapping("deleteById")
    public Result deleteById(Integer id) {
        Boolean success = checkGroupService.deleteById(id);
        if(success){
            log.info("删除检查组成功{}", id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }else {
            log.info("删除检查组失败{}", id);
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 编辑检查组
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, @RequestParam Integer[] checkitemIds) {
        checkGroupService.edit(checkGroup, checkitemIds);
        log.info("编辑检查组成功{}", checkGroup);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("分页查询{}", queryPageBean);
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        return checkGroupService.findPage(queryString, currentPage, pageSize);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupService.findAll());
    }

    /**
     * 根据套餐id获得检查组id
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckItemIdsByCheckGroupId(Integer setmealId) {
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkGroupService.findCheckGroupIdsBySetmealId(setmealId));
    }
}
